#!/usr/bin/env groovy

def call(Map config = [:]) {

    config = readYaml text: "${env.PIPELINE_CONFIG}"

    pipeline {
        agent {
            label "${config.agent_label}"
        }
        options{
            buildDiscarder(logRotator(numToKeepStr: '3'))//最多保留3次构建结果
            timeout(time: 1, unit: 'HOURS')
        }
        parameters {
            booleanParam(name: 'dev_spaces', defaultValue: false, description: '部署开发环境,仅对RP分支生效')
            booleanParam(name: 'preview_spaces', defaultValue: false, description: '部署集成环境,仅对RP分支生效')
            booleanParam(name: 'bug_spaces', defaultValue: false, description: '部署BUG环境,仅对RP分支生效')
            booleanParam(name: 'demo_spaces', defaultValue: false, description: '部署demo环境,仅对RP分支生效')
            booleanParam(name: 'test_spaces', defaultValue: false, description: '部署准生产环境,仅对Master分支生效')
            booleanParam(name: 'deploy_aliyun', defaultValue: false, description: '上传阿里云,仅对Master分支生效。需要提前根据APP_NAME创建仓库')
            string(name: 'buildRecordId', description: '构建记录号')
        }
        environment {
            //ORG为整体项目名称，例示：klx为产品项目
            //ORG               = "${config.project.code}"
            PROJECT_NAME = "${config.project.code}"
            ORG               = "klx"
            //APP_NAME为该项目的的名称，也为镜像的名称
            //APP_NAME          = "${config.project.appName}"
//            CHARTMUSEUM_CREDS = credentials('chartmuseum')
            //PREVIEW_NAMESPACE = "${config.build.namespace_SIT}"
            //TEST_NAMESPACE = "${config.build.namespace_UAT}"
            //DEV_NAMESPACE = "${config.build.namespace_DEV}"
            buildVersion = " "
            //spaces = ' '
            test404 = ' '
            CHARTMUSEUM_CREDS = credentials('chartmuseum')
        }
        stages {
            stage('获取K8S构建相关代码') {
                when{
                    expression { return params.buildRecordId }
                }
                steps{
                    script{
                        try{
                            k8sconfig k8s: config.k8s, project: config.project, scmCredentialsId: 'gitee-admin'
                        } catch (Exception e) {
                            error(e)
                        }
                    }
                }
            }
            stage('构建java代码') {
                steps {
                    script {
                        try{
                            buildVersion = javaBuild build: config.build, jvmCacertsEnable: config.k8s.jvmCacerts.enabled, deployEnable: config.k8s.deploy.enabled
                        } catch (Exception e) {
                            error(e)
                        }
                    }
                }
            }
            stage('构建镜像') {
                steps {
                    script {
                        try{
                            buildJavaImage version: env.PREVIEW_VERSION, deploy_aliyun: params.deploy_aliyun
                        } catch (Exception e) {
                            error(e)
                        }
                    }
                }
            }
            stage('应用部署') {
                steps {
                    script {
                        try{
                            deployImage deployEnable: config.k8s.deploy.enabled, deploy_aliyun: params.deploy_aliyun, jvmCacertsEnable: config.k8s.jvmCacerts.enabled
                        } catch (Exception e) {
                            error(e)
                        }
                    }
                }
            }
        }

        post {
            always {
                //input """调试断点.
                //    将注释打开后进入调试模式"""
                cleanWs()
                script{
                    codeplatform.build_callback(buildRecordId,buildVersion)
                }
            }
        }
    }
}

